package com.example.zs.mytest;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.example.zs.mytest.adapter.MyAdapter;
import com.example.zs.mytest.util.IsNetConnet;
import com.example.zs.mytest.view.MultiStateView;
import com.example.zs.mytest.view.MySwipeRefreshLayout;
import com.example.zs.mytest.view.XRecyclerView.ProgressStyle;
import com.example.zs.mytest.view.XRecyclerView.XRecyclerView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private MySwipeRefreshLayout swipeLayout;
    private MultiStateView multiStateView;
    private XRecyclerView recyclerView;
    private ArrayList<String> mData = new ArrayList<>();
    private MyAdapter mAdaper;

    private int mPage;
    private int mAllPage = 4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_layout);
        swipeLayout = (MySwipeRefreshLayout) findViewById(R.id.swipelayout);
        multiStateView = (MultiStateView) findViewById(R.id.multiStateView);
        recyclerView = (XRecyclerView) findViewById(R.id.recycleView);
        mAdaper = new MyAdapter(this);
        for (int i = 0; i < 10; i++) {
            mData.add("数据" + i);
        }
        swipeLayout.setRefreshing(false);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this,2);
        recyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setPullRefreshEnabled(false);
        recyclerView.setAdapter(mAdaper);
        mAdaper.bindData(mData);

        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {
                mPage ++;
                setData();

            }
        });

        swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                recyclerView.reset();
                mPage = 0;
                setData();
            }
        });

        recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
                if (manager instanceof GridLayoutManager){

                    View view = recyclerView.getChildAt(0);
                    if (manager.getDecoratedTop(view) == 0){
                        swipeLayout.setEnabled(true);
                    }else{
                        swipeLayout.setEnabled(false);
                    }
                }


            }

            /*@Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }*/

        });

    }

    /**
     * 设置数据
     */
    private void setData(){

        if (!IsNetConnet.isNetworkAvailable(this)){
            multiStateView.setViewState(MultiStateView.VIEW_STATE_EMPTY);
            swipeLayout.setRefreshing(false);
            Toast.makeText(this, "没有网络", Toast.LENGTH_SHORT).show();
            mPage--;
            return;
        }

        if (mPage == 0){
            mData.clear();
            for (int i = 0; i < 10; i++) {
                mData.add("数据" + i);
            }
        }else{

            if (mPage < mAllPage){
                for (int i = 0; i < 10 ; i++) {
                    mData.add("数据" + (i + mPage * 10));
                }
            }
        }
        multiStateView.setViewState(MultiStateView.VIEW_STATE_LOADING);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (mPage == 0){
                    mAdaper.bindData(mData);
                    swipeLayout.setRefreshing(false);
                }else{
                    if (mPage == mAllPage){
                    }else{
                        mAdaper.addData(mData);
                        recyclerView.loadMoreComplete();
                    }
                }
                multiStateView.setViewState(MultiStateView.VIEW_STATE_CONTENT);

            }
        },2000);

    }

//    class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{
//
//
//        private Context mContext;
//        private List<String> dataList = new ArrayList<>();
//
//        public MyAdapter(Context context) {
//            this.mContext = context;
//        }
//
//        @Override
//        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//            View view = LayoutInflater.from(mContext).inflate(R.layout.list_item_news_evaluate_grid,null);
//            MyViewHolder holder = new MyViewHolder(view);
//            return holder;
//        }
//
//
//        @Override
//        public void onBindViewHolder(MyViewHolder holder, int position) {
//
//            String title = dataList.get(position);
//            holder.text.setText(title);
//            Picasso.with(mContext).load("https://testwww.ibaodian.com/imgupload/information/despic/21501481510821167.jpg").into(holder.image);
//
//        }
//
//        @Override
//        public int getItemCount() {
//            return dataList.size();
//        }
//
//        public void bindData(ArrayList<String> data){
//            if (dataList != null && dataList.size() > 0) {
//                dataList.clear();
//            }
//            dataList = (List<String>) data.clone();
//            notifyDataSetChanged();
//
//        }
//
//        public void addData(ArrayList<String> data){
//
//            if (data.size() > dataList.size()){
//                for (int i = dataList.size(); i < data.size(); i++) {
//                    dataList.add(data.get(i));
//                }
//            }
//            notifyDataSetChanged();
//
//        }
//
//        class MyViewHolder extends RecyclerView.ViewHolder{
//
//            private ImageView image;
//            private TextView text;
//
//            public MyViewHolder(View itemView) {
//                super(itemView);
//                image = (ImageView) itemView.findViewById(R.id.iv_news);
//                text = (TextView) itemView.findViewById(R.id.titile_news);
//
//            }
//        }
//    }
}
